package nju.bean.meeting.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.BorderFactory;

@SuppressWarnings("serial")
public class NoBorderButton extends JButton {
	
	public static void main(String agrs[]){
		JFrame jf = new JFrame();
		JButton jb = new NoBorderButton();
		jb.setText("nobutton");
		jf.add(jb);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
	}
	
	NoBorderButton(){
		init();
	}
	
	
	private void init(){

	    this.setOpaque(false);
	    this.setBorder(emptyBorder);
	    this.setContentAreaFilled(false);
	    this.setFocusPainted(false);
	    this.setRolloverEnabled(true);
	    
	    this.setFocusable(true);
	
	    this.removeFocusListener(this.getFocusListeners()[0]);
	    
	    this.addMouseListener(new MouseAdapter() {
	
	        @Override
	        public void mouseEntered(MouseEvent e) {
	            if (isRolloverEnabled()&&isEnabled()) {
	                setBorder(roverBorder);
	            }
	        }
	
	        @Override
	        public void mouseExited(MouseEvent e) {
	            if (isRolloverEnabled()&&isEnabled()) {
	                setBorder(emptyBorder);
	            }
	        }
	    });
	    
	    
	    this.addFocusListener(new FocusListener(){

			@Override
			public void focusGained(FocusEvent e) {
	            if (isRolloverEnabled()&&isEnabled()) {
	                setBorder(roverBorder);
	            }
			}

			@Override
			public void focusLost(FocusEvent e) {
	            if (isRolloverEnabled()&&isEnabled()) {
	                setBorder(emptyBorder);
	            }
			}});
	    
	}

	private Border emptyBorder = BorderFactory.createEmptyBorder(0,0, 0, 0);  
	
	private Color roverBorderColor = Color.gray;   
	private Border roverBorder = new Border() {   
	  
	            @Override
				public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {   
	                g.setColor(roverBorderColor);   
	                g.drawRect(x, y, width - 1, height - 1);   
	            }   
	  
	            @Override
				public Insets getBorderInsets(Component c) {   
	                return new Insets(1, 1, 1, 1);   
	            }   
	  
	            @Override
				public boolean isBorderOpaque() {   
	                return true;   
	            }   
	        };  

}
